package com.example.restaurantclient.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTabHost;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.restaurantclient.Bubble.BadgeView;
import com.example.restaurantclient.Fragment.CartFragment;
import com.example.restaurantclient.Fragment.HomeFragment;
import com.example.restaurantclient.Fragment.NewsFragment;
import com.example.restaurantclient.Fragment.PersonnalFragment;
import com.example.restaurantclient.R;

import java.util.HashMap;
import java.util.Map;

public class FragmentActivity extends AppCompatActivity {
    private Map<String, ImageView> imageIconViewMap=new HashMap<>();//多页面图标
    private Map<String, TextView> textViewMap=new HashMap<>();//多页面图标文字
    //消息提醒气泡
    private BadgeView bv1;
    //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        //获取FragmentHost的引用
        FragmentTabHost fragmentTabHost=findViewById(android.R.id.tabhost);
        //初始化
        fragmentTabHost.setup(this,getSupportFragmentManager(),android.R.id.tabcontent);
        //创建内容页面TabSpec对象
        TabHost.TabSpec tab1=fragmentTabHost.newTabSpec("restaurant_home_tab").setIndicator(getTabSpecView("restaurant_home_tab","首页",R.drawable.home1));
        //Class参数：类名.class,对象.getClass()，Class.forName()
        fragmentTabHost.addTab(tab1,
                HomeFragment.class,//FirstFragment类的class对象
                null);//传递数据时使用，不需要传递数据直接传null
        TabHost.TabSpec tab2=fragmentTabHost.newTabSpec("restaurant_news_tab").setIndicator(getTabSpecView("restaurant_news_tab","消息",R.drawable.news1));
        //Class参数：类名.class,对象.getClass()，Class.forName()
        fragmentTabHost.addTab(tab2,
                NewsFragment.class,//FirstFragment类的class对象
                null);//传递数据时使用，不需要传递数据直接传null
        TabHost.TabSpec tab3=fragmentTabHost.newTabSpec("restaurant_cart_tab").setIndicator(getTabSpecView("restaurant_cart_tab","购物车",R.drawable.cart1));
        //Class参数：类名.class,对象.getClass()，Class.forName()
        fragmentTabHost.addTab(tab3,
        CartFragment.class,//FirstFragment类的class对象
                null);//传递数据时使用，不需要传递数据直接传null
        TabHost.TabSpec tab4=fragmentTabHost.newTabSpec("restaurant_person_tab").setIndicator(getTabSpecView("restaurant_person_tab","我的中心",R.drawable.person1));
        fragmentTabHost.addTab(tab4,
                PersonnalFragment.class,//FirstFragment类的class对象
                null);//传递数据时使用，不需要传递数据直接传null
        //设置气泡
//        bv1=new BadgeView(this);
//        bv1.setBadgeText(3).setBindView(imageIconViewMap.get("restaurant_news_tab"));
//        bv1.setTextColor(getResources().getColor(R.color.colorWhite));
//        bv1.setBadgeLayoutParams(15,15);
//        bv1.setTextSize(10);
        //处理fragmentTabHost的选项切换事件
        fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                //修改图片和文字的颜色
                switch (tabId){
                    case "restaurant_home_tab"://选中了菜系分类
                        imageIconViewMap.get("restaurant_home_tab").setImageResource(R.drawable.home2);
                        imageIconViewMap.get("restaurant_news_tab").setImageResource(R.drawable.news1);
                        imageIconViewMap.get("restaurant_cart_tab").setImageResource(R.drawable.cart1);
                        imageIconViewMap.get("restaurant_person_tab").setImageResource(R.drawable.person1);
                        textViewMap.get("restaurant_home_tab").setTextColor(getResources().getColor(R.color.colorOrangeRed));
                        textViewMap.get("restaurant_news_tab").setTextColor(getResources().getColor(R.color.colorGrayBlake));
                        textViewMap.get("restaurant_cart_tab").setTextColor(getResources().getColor(R.color.colorGrayBlake));
                        textViewMap.get("restaurant_person_tab").setTextColor(getResources().getColor(R.color.colorGrayBlake));
                        break;
                    case "restaurant_news_tab":
                        imageIconViewMap.get("restaurant_home_tab").setImageResource(R.drawable.home1);
                        imageIconViewMap.get("restaurant_news_tab").setImageResource(R.drawable.news2);
                        imageIconViewMap.get("restaurant_cart_tab").setImageResource(R.drawable.cart1);
                        imageIconViewMap.get("restaurant_person_tab").setImageResource(R.drawable.person1);
                        textViewMap.get("restaurant_home_tab").setTextColor(getResources().getColor(R.color.colorGrayBlake));
                        textViewMap.get("restaurant_news_tab").setTextColor(getResources().getColor(R.color.colorOrangeRed));
                        textViewMap.get("restaurant_cart_tab").setTextColor(getResources().getColor(R.color.colorGrayBlake));
                        textViewMap.get("restaurant_person_tab").setTextColor(getResources().getColor(R.color.colorGrayBlake));
                        break;
                    case "restaurant_cart_tab":
                        imageIconViewMap.get("restaurant_home_tab").setImageResource(R.drawable.home1);
                        imageIconViewMap.get("restaurant_news_tab").setImageResource(R.drawable.news1);
                        imageIconViewMap.get("restaurant_cart_tab").setImageResource(R.drawable.cart2);
                        imageIconViewMap.get("restaurant_person_tab").setImageResource(R.drawable.person1);
                        textViewMap.get("restaurant_home_tab").setTextColor(getResources().getColor(R.color.colorGrayBlake));
                        textViewMap.get("restaurant_news_tab").setTextColor(getResources().getColor(R.color.colorGrayBlake));
                        textViewMap.get("restaurant_cart_tab").setTextColor(getResources().getColor(R.color.colorOrangeRed));
                        textViewMap.get("restaurant_person_tab").setTextColor(getResources().getColor(R.color.colorGrayBlake));
                        break;
                    case "restaurant_person_tab":
                        imageIconViewMap.get("restaurant_home_tab").setImageResource(R.drawable.home1);
                        imageIconViewMap.get("restaurant_news_tab").setImageResource(R.drawable.news1);
                        imageIconViewMap.get("restaurant_cart_tab").setImageResource(R.drawable.cart1);
                        imageIconViewMap.get("restaurant_person_tab").setImageResource(R.drawable.person2);
                        textViewMap.get("restaurant_home_tab").setTextColor(getResources().getColor(R.color.colorGrayBlake));
                        textViewMap.get("restaurant_news_tab").setTextColor(getResources().getColor(R.color.colorGrayBlake));
                        textViewMap.get("restaurant_cart_tab").setTextColor(getResources().getColor(R.color.colorGrayBlake));
                        textViewMap.get("restaurant_person_tab").setTextColor(getResources().getColor(R.color.colorOrangeRed));
                        break;
                }
            }
        });
        //设置默认选中的标签页:参数是下标
        fragmentTabHost.setCurrentTab(0);
        imageIconViewMap.get("restaurant_cart_tab").setImageResource(R.drawable.cart2);
        textViewMap.get("restaurant_cart_tab").setTextColor(getResources().getColor(R.color.colorOrangeRed));

        Intent intent=getIntent();
        if(intent.getAction()==Intent.ACTION_VIEW){
            fragmentTabHost.setCurrentTab(2);
            imageIconViewMap.get("restaurant_home_tab").setImageResource(R.drawable.home2);
            textViewMap.get("restaurant_home_tab").setTextColor(getResources().getColor(R.color.colorOrangeRed));
        }
    }
    public View getTabSpecView(String tag, String title, int drawable){
        View view= getLayoutInflater().inflate(R.layout.tab_spec_layout,null);
        //获取tab_spec_layout布局当中视图控件的引用
        ImageView icon=view.findViewById(R.id.icon);
        icon.setImageResource(drawable);
        //将imageView对象存储在Map中
        imageIconViewMap.put(tag,icon);

        TextView tvTitle=view.findViewById(R.id.title);
        tvTitle.setText(title);
        textViewMap.put(tag,tvTitle);

        return view;
    }
}
